<?php
namespace app\common\logic;
use app\admin\model\system\Node;
use app\common\service\NodeService;
use think\Db;

/**
 * Class MenuLogic 菜单相关逻辑
 * @package app\common\logic
 */
class MenuLogic
{

    /**
     * @Notes:导入节点
     * @User: chenping
     * @email: 375129701@qq.com
     * @param $apps array
     * @return array
     */
    public static function importMenu($apps)
    {
        $newNodes = [];

        foreach ($apps as $app) {

            $nodeLists = NodeLogic::getAppNodes($app);

            if ($nodeLists) {

                $newNodes[$app] = [];

                foreach ($nodeLists as $k => $v) {

                    //层级关系处理

                    if($v['is_menu']==1)
                    {
                        //检测节点是否存在
                        $node = Db::name("auth_menu")->where("name", $v["name"])->find();

                        if (!empty($node)) {
                            // array_push($newNodes[$app], $v['name'] . "--已导入");

                        } else {
                            //顶级菜单
                            if($v['MenuRoot']=='default')
                            {
                                //新增节点
                                $result = Db::name("auth_menu")->insert([
                                    "title"     => $v['title'],
                                    "name"      => $v["name"],
                                    "status"    => $v['status'],
                                    "type"      => $v['type'],
                                    "parent_id" => 0,
                                    "condition" => $v["condition"],
                                    "is_view"   => $v["is_view"],
                                    "url_link"  => $v['url_link'] ? $v['url_link'] : $v['name'],
                                    "icon"      => isset($v['icon'])? $v['icon'] : ""
                                ]);
                                if ($result) {

                                    array_push($newNodes[$app], $v['name'] . "--导入成功");

                                } else {

                                    array_push($newNodes[$app], $v['name'] . "--导入失败");
                                }
                            }
                            //子集菜单
                            if($v["MenuRoot"]!='default')
                            {
                                $sonMenu[]= $v;

                                $NodeParent = Db::name("auth_menu")->where("name",$v["MenuRoot"])->field("id")->find();

                                $result = Db::name("auth_menu")->insert([
                                    "title"     => $v['title'],
                                    "name"      => $v["name"],
                                    "status"    => $v['status'],
                                    "type"      => $v['type'],
                                    "parent_id" => $NodeParent['id'] ? $NodeParent['id'] :0,
                                    "condition" => $v["condition"],
                                    "is_view"   => $v["is_view"],
                                    "url_link"  => $v['url_link'] ? $v['url_link'] : $v['name'],
                                    "icon"      => isset($v['icon'])? $v['icon'] : ""

                                ]);
                                if ($result) {

                                    array_push($newNodes[$app], $v['name'] . "--导入成功");

                                } else {

                                    array_push($newNodes[$app], $v['name'] . "--导入失败");
                                }
                            }
                        }
                    }
                }
                //子集菜单
               /* if(isset($sonMenu) && !empty($sonMenu))
                {
                    //层级检测
                    foreach ($sonMenu as $son)
                    {
                        $NodeParent = Db::name("auth_menu")->where("name",$son["MenuRoot"])->field("id")->find();

                        $result = Db::name("auth_menu")->insert([
                            "title"     => $son['title'],
                            "name"      => $son["name"],
                            "status"    => $son['status'],
                            "type"      => $son['type'],
                            "parent_id" => $NodeParent['id'] ? $NodeParent['id'] :0,
                            "condition" => $son["condition"],
                            "is_view"   => $son["is_view"],
                            "url_link"  => $son['url_link'] ? $son['url_link'] : $son['name']
                        ]);
                        if ($result) {

                            array_push($newNodes[$app], $son['name'] . "--导入成功");

                        } else {

                            array_push($newNodes[$app], $son['name'] . "--导入失败");
                        }
                    }
                }*/
            }
        }
        return $newNodes;
    }


    /**
     * @Notes:更新系统节点
     * @User: chenping
     * @email: 375129701@qq.com
     * @param $apps array
     * @return array
     */
    public static function  updateMenu($apps)
    {
        $newNodes = [];

        foreach ($apps as $app) {

            $nodeLists = NodeLogic::getAppNodes($app);

            if ($nodeLists) {

                $newNodes[$app] = [];

                foreach ($nodeLists as $k => $v) {

                    if($v['is_menu']==1)
                    {
                        //检测节点
                        $node = Db::name("auth_menu")->where("name", $v["name"])->find();

                        if ($node) {

                            //节点层级变化
                            $NodeParent = Db::name("auth_menu")->where("name", $v["MenuRoot"])->find();

                            $result = Db::name("auth_menu")->where("id", $node['id'])->update([
                                "title"     => $v['title'],
                                "name"      => $v["name"],
                                "status"    => $v['status'],
                                "type"      => $v['type'],
                                "parent_id" => $NodeParent['id'] ? $NodeParent['id'] : 0,
                                "condition" => $v["condition"],
                                "is_view"   => $v["is_view"],
                                "url_link"  => $v['url_link'] ? $v['url_link'] : $v['name'],
                                "icon"      => isset($v['icon']) ? $v['icon'] : ""
                            ]);

                            if ($result) {

                                array_push($newNodes[$app], $v['name'] . "--已更新");

                            } else {

                                array_push($newNodes[$app], $v['name'] . "--无需更新");
                            }
                        }else{
                            array_push($newNodes[$app], $v['name'] . "--需要导入");
                        }
                    }
                }
            }
        }
        return $newNodes;
    }

    /**
     * @Notes:树形菜单
     * @User: chenping
     * @email: 375129701@qq.com
     * @param $data
     * @return array
     */
    public function getTree($data)
    {

        $items = array();
        foreach ($data as $v)
        {
            $v['href'] = url($v['href']);
            $items[$v['id']] = $v;
        }

        $tree = [];
        foreach ($items as $k => $item)
        {
            if (isset($items[$item['parent_id']]))
            {
                $items[$item['parent_id']]['hasChildren'] = 1;
                $items[$item['parent_id']]['child'][] = &$items[$k];
            } else {
                $tree[] = &$items[$k];
            }
        }
        return $tree;
    }

    /**
     * @Notes: 获取子集菜单
     * @User: chenping
     * @email: 375129701@qq.com
     * @param $arr
     * @param $id
     * @return array
     */
    public function getTreeSon($arr,$id)
    {
        //初始化一次
        static $new_arr =[];
        foreach ($arr as $k=>$v)
        {
            if($v["parent_id"]==$id)
            {
                $new_arr[] = $v;
                $this->getTreeSon($arr,$v['id']);
            }
        }
        return $new_arr;
    }
}